import flash.events.SQLEvent;
import flash.filesystem.File;

import forms.Utils;

import mx.collections.ArrayList;
import mx.events.CollectionEvent;

	//SOS! Uncomment this before release!
	//var dbFile:File = File.applicationDirectory.resolvePath("LiveScoresDB.db");
	var dbFile:File = File.desktopDirectory.resolvePath("LiveScoresDB.db");

var dbTemplate:File = new File();
private  var conn:SQLConnection = new SQLConnection();
private  var createStmt:SQLStatement = new SQLStatement();
private  var selectStmt:SQLStatement = new SQLStatement();
private  var insertStmt:SQLStatement = new SQLStatement();
private  var deleteStmt:SQLStatement = new SQLStatement();
public var result:SQLResult = new SQLResult();
public function initDB()
{
	if (!dbFile.exists)
	{
		//dbTemplate = File.applicationStorageDirectory.resolvePath("DBTemplate.db");
		dbTemplate = File.desktopDirectory.resolvePath("DBTemplate.db");
		dbTemplate.copyTo(dbFile);
	}
}

public  function Init()
{
	try
	{
		conn.open(dbFile);
		
		
	}
	catch (error:SQLError)
	{
		trace("Error message:", error.message);
		trace("Details:", error.details);
	}
}



//adding gib ...
public  function addGibInfo(name,phone,mphone,mail,favStatus,tweetName)
{
	//deleteNow();
	insertStmt.sqlConnection = conn;
	var today_date:Date = new Date();
	var sql:String = 
		"INSERT INTO GibView1 (date, month, year,name,phone,mphone,mail,fav, tweetName) " + 
		"VALUES (:date, :month, :year,:name,:phone, :mphone, :mail,:fav,:tweetName)";
	insertStmt.text = sql;
	//insertStmt.parameters[":ID"] = add()
	var y = new Date().fullYear;
	var date:Date = new Date();
	insertStmt.parameters[":month"] = date.getMonth();
	insertStmt.parameters[":date"] = date.date;
	insertStmt.parameters[":year"] = y;
	insertStmt.parameters[":name"] =  name;
	insertStmt.parameters[":phone"] = phone;
	insertStmt.parameters[":mphone"] = mphone;
	insertStmt.parameters[":mail"] = mail;
	insertStmt.parameters[":fav"] = favStatus;
	insertStmt.parameters[":tweetName"] = tweetName;
	try
	{
		// execute the statement
		insertStmt.execute();
		
		//Alert.show("All done!");
		//nowShow();
		
		
	}
	catch (error:SQLError)
	{
		trace("Error occured on insert: "+error.details);//Alert.show((error.details).toString());
	}
}

// done in the nowShow function
public function filterGibs(params)
{
	
}

public function editGib(data,newData)
{
	deleteNow(data.uid,true);
	addGibInfo(newData.name,newData.phone,newData.mphone,newData.mail,newData.fav,newData.tweetName);
}

////////////// DELETE/CHANGE FUNCTIONS ///////////////////////
//////////////////////////////////////////////////////////////

public  function deleteNow(uid,silent=false):void
{
	deleteStmt.sqlConnection = conn;
	
	deleteStmt.text = "DELETE FROM GibView1 WHERE uid = :param1";
	deleteStmt.parameters[":param1"] = uid;
	
	// This try..catch block is fleshed out in
	// a subsequent code listing
	try
	{
		deleteStmt.execute();
	}
	catch (error:SQLError)
	{
		trace("Error on delete: "+error.details);//Alert.show("no data!");
	}
		//nowShow();
}

public  function changeFavStatus(uid,status,info:Object)
{
	deleteNow(uid,true);
	addGibInfo(info.name,info.phone,info.mphone,info.mail,status,info.tweetName);
}